package game.common
{
	import flash.net.NetConnection;
	import flash.net.Responder;
	
	import game.managers.ConfigManager;
	
	/**
	 * NetConnect类,用来与amfphp之间交互
	 * */
	public class NetConnect
	{
		private static var _instance:NetConnect;
		
		private var _net:NetConnection;
		public function NetConnect()
		{
			if (!_instance) 
			{
				_instance=this;
				init();
			}else{
				throw new Error("NetConnect is a single class");
			}
		}
		
		public static function getInstance():NetConnect
		{
			if (!_instance) 
			{
				_instance = new NetConnect();
			}
			return _instance;
		}
		
		private function init():void
		{
			if (!_net) 
			{
				_net = new NetConnection();
				_net.connect(ConfigManager.AMFPHP);
			}
		}
		
		
		/**
		 * index--当食物数量为0时才会赋值
		 * */
		public function call(param:Object,index:int = 0):void
		{
			if (param == null) 
			{
				_net.call(ConfigManager.AMFPHP_SERVER,new Responder(onSuccess,onError),
					Global.nowEx,Global.money,Global.totalEx,Global.level,"null","null","null","null",index);
			}else{
				_net.call(ConfigManager.AMFPHP_SERVER,new Responder(onSuccess,onError),
					Global.nowEx,Global.money,Global.totalEx,Global.level,param.source,param.num,param.price,param.ex,param.index);
			}
		}
		
		private function onSuccess(e:Object):void
		{
			trace(e as String);
		}
		
		private function onError(e:Object):void
		{
			trace(e.toString() + "error");	
		}
	}
}